/*
Copyright 2022.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
	"fmt"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE!  THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required.  Any new fields you add must have json tags for the fields to be serialized.

type ScopeSpec struct {
	//+kubebuilder:default=mutation
	Type  string `json:"type,omitempty"`
	Items []Item `json:"items,flow"`
}

// Item ScopeItems defines items of Scope
type Item struct {
	Name  string `json:"name"`
	Match Match  `json:"match,omitempty"`
	Rule  Rule   `json:"rule,omitempty"`
}

type Rule struct {
	Simples []Simple `json:"simples,omitempty"`
	Rego    Rego     `json:"rego,omitempty"`
}

type Rego struct {
	Template   string `json:"template"`
	Parameters string `json:"parameters,omitempty"`
}

type Simple struct {
	Name  string `json:"name"`
	Path  string `json:"path"`
	Value string `json:"value"`

	// +kubebuilder:validation:Enum=allow;deny
	// +kubebuilder:default=deny
	Action  string `json:"action,omitempty"`
	Message string `json:"message,omitempty"`
}

//+kubebuilder:object:root=true

// Scope is the Schema for the Scopes API
// +kubebuilder:subresource:status
type Scope struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`
	Spec              ScopeSpec   `json:"spec,omitempty"`
	Status            ScopeStatus `json:"status,omitempty"`
}

type ScopeStatus struct {
	// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
	// Important: Run "make" to regenerate code after modifying this file
	Phase      Phase       `json:"phase,omitempty" protobuf:"bytes,1,opt,name=phase,casttype=Phase"`
	Conditions []Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,2,rep,name=conditions"`
}

//+kubebuilder:object:root=true

// ScopeList contains a list of Scope
type ScopeList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []Scope `json:"items"`
}

func init() {
	SchemeBuilder.Register(&Scope{}, &ScopeList{})
}

func (in *Scope) GetNamespacedName() string {
	return fmt.Sprintf("%s/%s", in.Namespace, in.Name)
}
